package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type ForbidSendMsgStruct struct {
	NickName string `json:"NickName"`
	ForbidSendMsgRequest
}

type ForbidSendMsgRequest struct {
	GroupID        string   `json:"GroupId"`         // 需要查询的群组 ID
	MembersAccount []string `json:"Members_Account"` // 需要禁言的用户帐号，最多支持500个帐号
	MuteTime       int      `json:"MuteTime"`        // 无符号类型。需禁言时间，单位为秒，为0时表示取消禁言，4294967295为永久禁言。
}

type ForbidSendMsgResponse struct {
	ActionStatus string `json:"ActionStatus"`
	ErrorInfo    string `json:"ErrorInfo"`
	ErrorCode    int    `json:"ErrorCode"`
}

// ForbidSendMsg 禁言/取消禁言 替代banuser
func (s sdk) ForbidSendMsg(req ForbidSendMsgRequest) bool {
	reqUrl := s.BuildRestfulUrl("/group_open_http_svc/forbid_send_msg")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("ForbidSendMsg error: ", err)
		return false
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	resp := ForbidSendMsgResponse{}
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("ForbidSendMsg Unmarshal error: ", err)
		return false
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("ForbidSendMsg  error: ", err, "error message : ", resp.ErrorInfo)
		return false
	}
	return true
}
